<template>
<LayoutNav></LayoutNav>
<div class="container">
  <div class="search-page">
    <!-- 搜索类别切换 -->
    <div class="search-types">
      <el-radio-group 
        v-model="activeType" 
        fill="#FA5566"
        v-if="availableTypes.length > 0"
        @change="handleTypeChange"
      >
        <el-radio-button 
          v-for="type in availableTypes" 
          :key="type.value"
          :label="type.value"
        >
          {{ type.label }}
        </el-radio-button>
      </el-radio-group>
      
      <!-- 当没有可用搜索类型时显示提示 -->
      <el-empty 
        v-else
        description="暂无可用的搜索功能"
      />
    </div>
      <!-- 路由出口 -->
      <router-view></router-view>
  </div>
</div>
    
<LayoutFooter/>
</template>

<script setup>
import { ref, computed } from 'vue'
import LayoutNav from '../Layout/components/LayoutNav.vue';
import LayoutFooter from '../Layout/components/LayoutFooter.vue';
import { useTokenStore } from '@/store/token';
import { useRouter } from 'vue-router' // 确保引入useRouter

const router = useRouter()
// 用户信息
const tokenStore = useTokenStore()
const user = tokenStore.user;

// 根据用户身份获取可用的搜索类型
const availableTypes = computed(() => {
  const identity = user.identity
  switch(identity) {
    case '0': // 普通会员
      return [{
        label: '找实体店',
        value: 'store'
      }]
    case '1': // 实体店
      return [
        {
          label: '找服装',
          value: 'clothes'
        },
        {
          label: '找批发商',
          value: 'wholesale'
        },
        {
          label: '找加工厂',
          value: 'factory'
        },
        {
          label: '众筹浏览',
          value: 'crowdfunding'
        }
      ]
    case '2': // 批发商
      return [
        {
          label: '找加工厂',
          value: 'factory'
        },
        {
          label: '众筹浏览',
          value: 'crowdfunding'
        }
      ]
    case '3': // 加工厂
      return [] // 什么都不能看
    default:
      return []
  }
})

// 设置默认选中的类型(取第一个可用类型,如果没有则为空)
const activeType = ref(availableTypes.value[0]?.value || '')

// 处理类型切换
const handleTypeChange = (type) => {
  router.push(`/search/${type}`)
}

// 初始化时跳转到默认搜索类型
if (activeType.value) {
  router.push(`/search/${activeType.value}`)
}

</script>

<style scoped lang="scss">
.search-page {
  padding: 20px;
}

.search-types {
  margin-bottom: 20px;
  text-align: center;
  :deep(.el-radio-group) {
    .el-radio-button {
      margin-right: 20px;
      
      &:last-child {
        margin-right: 0;
      }
      
      .el-radio-button__inner {
        border: none;
        background: none;
        padding: 0 10px;
        height: 40px;
        line-height: 40px;
        color: #666;
        font-size: 16px;
        // 移除默认背景色和边框
        background-color: transparent !important; // 使用 !important 确保覆盖
        border-color: transparent !important;
        box-shadow: none !important;
        
        &:hover {
          color: #FA5566;
        }
      }
      
      &.is-active {
        .el-radio-button__inner {
          color: #FA5566;
          box-shadow: none;
          position: relative;          
          &::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 50px;
            height: 2px;
            background: #FA5566;
            border-radius: 1px;
          }
        }
      }
    }
    
    // 移除默认边框和圆角
    .el-radio-button:first-child .el-radio-button__inner {
      border: none;
      border-radius: 0;
    }
    
    .el-radio-button:last-child .el-radio-button__inner {
      border: none;
      border-radius: 0;
    }
  }
}

.search-box {
  max-width: 600px;
  margin: 0 auto 20px;
  display: flex;
  .search-input {
    width: 100%;
  }
  .el-button {
    margin-left: 20px;
    width: 80px;
  }
}

</style>